package tom.spring.cloud.amqp.rabbitmq.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author ZHUFEIFEI
 */
@Slf4j
@Service
public class MsgProducer {
    @Autowired
    private AmqpTemplate rabbit;

    /**
     * 1对1的Direct交换机类型
     * @param msg
     */
    public void sendDirect(String msg) {
        log.info("send direct msg : {}", msg);
        //这个默认发的queue不对
//        this.rabbit.convertAndSend(msg);
        this.rabbit.convertAndSend("hello", msg);
    }

    public void sendTopic(String msg) {
        log.info("send topic msg : {}", msg);
        //exchange与topic.message 和 topic.#绑定，所以消息会发到topic.message和topic.messages两个队列中
        this.rabbit.convertAndSend("exchange", "topic.message", msg);
    }

    public void sendFanout(String msg) {
        log.info("send fanout msg : {}", msg);
        //fanout类型的交换机，第二个参数无效，不用设置
        this.rabbit.convertAndSend("fanoutExchange", "", msg);
    }

}
